package org.journal.service;

import org.journal.dto.UserDTO;
import org.journal.security.Hasher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

@Service
public class SecurityServiceImpl implements SecurityService {

	private final static Logger log = LoggerFactory
			.getLogger(SecurityServiceImpl.class);

	public boolean isAuthorized() {

		Authentication authentication = SecurityContextHolder.getContext()
				.getAuthentication();

		return authentication != null
				&& !(authentication instanceof AnonymousAuthenticationToken)
				&& authentication.isAuthenticated();

	}

	public Integer getUserId() {
		Integer userId = null;
		if (isAuthorized()) {
			UserDTO user = (UserDTO) SecurityContextHolder.getContext()
					.getAuthentication().getPrincipal();
			userId = user.getId();
		}
		return userId;

	}

}
